/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pacificlms.MainPanel;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import pacificlms.DataHolder;
import pacificlms.MasterConnection;
import pacificlms.Verifiable.*;
import pacificlms.Verifiable.Database.*;
import pacificlms.Error.ErrorHandle;
import java.lang.reflect.Method;
import java.util.Calendar;
import javax.swing.DefaultListModel;
import pacificlms.LoggedMember;


/**
 * InformationMember is responsible for displaying, editing, and creating
 * Members, and to display fines
 * InformationMember also allows librarians to search members, print member 
 * cards and to pay fines
 * @author Brian Hume
 */
public class InformationMember extends CentralUIPanel
{
    Member CurrentMember = null;
    boolean NewMember = true;
    
    /**
     * Creates new form InformationMember
     * and fills the fields (if a VO is passed)
     */
    public InformationMember(VerifiableObject VO) {
        super(VO);
        initComponents();
        Create((Member)VO);
        if (VO != null) 
        {
            NewMember = false; 
        }
    }
    
    protected Method SetMethodRef;
    
    /**
     * Creates the displays the member info, fills the member type list, sets the member data filling method for searches
     */
    void Create(Member Memb)
    {
        CurrentMember = Memb;
        if (Memb == null)
            NewMember = true;
        else
            NewMember = false;
        FillMemberTypeList();
        FillFields();
        
        try
        {
            Method[] MethodList = this.getClass().getDeclaredMethods();
            
            for(Method ML: MethodList)
            {
                if (ML.getName().equals("SetMember"))
                    SetMethodRef = ML;
                System.out.println(ML.getName());
            }
            //Class[] Arglist = new Class[1];
            //Arglist[0] = VerifiableObject.class;
            //SetMethodRef = ReflectionHelper.getMethod this.getClass().getMethod("SetMethod", Arglist);
        }
        catch(Exception Ex)
        {
            System.out.println(Ex.toString());
            SetMethodRef = null;
        }
    }
    
    public String TabName()
    {
        return "Member Catalog";
    }
    
    /**
     * Fills all of the member types from the DataHolder
     */
    void FillMemberTypeList()
    {
        ArrayList<MemberType> MTL = DataHolder.GetMemberTypeList();
        for(MemberType MT: MTL)
        {
            cboMemberType.addItem(MT.Type(null));
        }
        cboMemberType.setSelectedIndex(1);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnJoin = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        btnSearch = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtLibraryCard = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtIdentityNumber = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtPhone = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtJoinDate = new javax.swing.JTextField();
        txtCardExpiry = new javax.swing.JTextField();
        cboMemberType = new javax.swing.JComboBox();
        btnViewFines = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstCheckouts = new javax.swing.JList();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        btnPrintCard = new javax.swing.JButton();
        txtPassword = new javax.swing.JPasswordField();
        txtPassword2 = new javax.swing.JPasswordField();

        setMaximumSize(new java.awt.Dimension(615, 535));
        setMinimumSize(new java.awt.Dimension(615, 535));
        setPreferredSize(new java.awt.Dimension(615, 535));

        btnJoin.setText("New Member");
        btnJoin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnJoinActionPerformed(evt);
            }
        });

        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        jLabel1.setText("Library Card #");

        txtLibraryCard.setEnabled(false);

        jLabel2.setText("Card Expiry");

        jLabel3.setText("Identity Number");

        txtIdentityNumber.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIdentityNumberActionPerformed(evt);
            }
        });

        jLabel4.setText("Password");

        jLabel5.setText("Confirm Password");

        jLabel6.setText("E-mail");

        jLabel7.setText("Name");

        jLabel8.setText("Phone #");

        jLabel9.setText("Checkout List");

        jLabel10.setText("Join Date");

        txtJoinDate.setEnabled(false);

        txtCardExpiry.setEnabled(false);

        btnViewFines.setText("View Fines");
        btnViewFines.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewFinesActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(lstCheckouts);

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 22)); // NOI18N
        jLabel11.setText("Member Catalog");

        jLabel12.setText("Member Type");

        btnPrintCard.setText("Print Card");
        btnPrintCard.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrintCardActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnJoin, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 175, Short.MAX_VALUE)
                        .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(btnClose, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(4, 4, 4)
                                .addComponent(txtLibraryCard, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(116, 116, 116)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(31, 31, 31)
                                .addComponent(txtCardExpiry, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(4, 4, 4)
                                .addComponent(txtIdentityNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(145, 145, 145)
                                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(31, 31, 31)
                                .addComponent(txtJoinDate, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtPassword2, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(4, 4, 4)
                                .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(4, 4, 4)
                                .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(4, 4, 4)
                                .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(4, 4, 4)
                                .addComponent(cboMemberType, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnViewFines, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnPrintCard, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel9)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 595, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel11)
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtLibraryCard, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCardExpiry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtIdentityNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtJoinDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel10))))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtPassword2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel6))
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel7))
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel8))
                    .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(jLabel12))
                    .addComponent(cboMemberType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnViewFines)
                    .addComponent(btnPrintCard))
                .addGap(18, 18, 18)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnJoin)
                    .addComponent(btnSearch)
                    .addComponent(btnSave)
                    .addComponent(btnClose))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Fills all of the fields on the member form
     * Sets what fields should be editable
     */
    void FillFields()
    {
        if (CurrentMember == null)
        {
            CurrentMember = new Member();
        }
        
        txtLibraryCard.setText(CurrentMember.LibraryCardNumber(null));
        txtCardExpiry.setText(CurrentMember.CardExpiry(null));
        txtIdentityNumber.setText(CurrentMember.IdentityNumber(null));
        txtJoinDate.setText(CurrentMember.JoinDate(null));
        txtPassword.setText(CurrentMember.Password(null));
        txtPassword2.setText(CurrentMember.Password(null));
        txtEmail.setText(CurrentMember.Email(null));
        txtName.setText(CurrentMember.Name(null));
        txtPhone.setText(CurrentMember.PhoneNumber(null));

        SetMemberType(CurrentMember.Type(null));
        ListCheckouts();
        
        if (LoggedMember.getInstance().IsLibrarian())
        {
            btnSearch.setEnabled(true);
            btnPrintCard.setEnabled(true);
            cboMemberType.setEnabled(true);
        }
        else
        {
            btnSearch.setEnabled(false);
            btnPrintCard.setEnabled(false);
            cboMemberType.setEnabled(false);
        }
    }
    
    /**
     * Finds all of the checkouts associated with the current member
     */
    void ListCheckouts()
    {
        String Name = CurrentMember.Name(null);
        lstCheckouts.setModel(new DefaultListModel());
        if (Name != null || Name == "")
        {
            ArrayList<VerifiableObject> CheckOutList = CurrentMember.GetRelatedData(CheckOut.class, 1000);
            DefaultListModel DLM = new DefaultListModel();
            if (CheckOutList == null)
                return;
            for(VerifiableObject VO: CheckOutList)
            {
                DLM.addElement(VO);
            }
            lstCheckouts.setModel(DLM);
            return;
        }
    }
    
    /**
     * Selects the member type matching the string value of member type
     */
    public void SetMemberType(String Type)
    {
        if (Type == null)
        {
            Type = (String)cboMemberType.getItemAt(1);//get student
        }
        int Max = cboMemberType.getItemCount();
        for (int itr = 0; itr < Max; itr++)
        {
            if (cboMemberType.getItemAt(itr).equals(Type))
            {
                cboMemberType.setSelectedIndex(itr);
                return;
            }
        }
    }
    
    /**
     * Creates a member from all of the fields on the panel and attempts to verify it
     */
    boolean RegenerateMember()
    {
        Member Member = new Member();
        
        Member.LibraryCardNumber(txtLibraryCard.getText());
        Member.CardExpiry(txtCardExpiry.getText());
        Member.IdentityNumber(txtIdentityNumber.getText());
        Member.JoinDate(txtJoinDate.getText());
        String Password1=txtPassword.getText(), Password2=txtPassword2.getText();
        if (!Password1.equals(Password2))
        {
            JOptionPane.showMessageDialog(null, "Error your password does not match the confirmation", "Database Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        Member.Password(txtPassword.getText());
        Member.Email(txtEmail.getText());
        Member.Name(txtName.getText());
        Member.PhoneNumber(txtPhone.getText());

        Member.Type((String)cboMemberType.getSelectedItem());
        boolean Created = Member.Verify();
        if(Created)
        {
            CurrentMember = Member;
        }
        return Created;
    }
    
    void NewMember()
    {
        NewMember = true;
        CurrentMember = null;
        FillFields();
    }
    
    private void btnJoinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnJoinActionPerformed
        NewMember();
    }//GEN-LAST:event_btnJoinActionPerformed
    
    void ValidateMemberID() throws Exception
    {
        if (txtIdentityNumber.getText().equalsIgnoreCase("IsFakeID"))
            throw new Exception("Identity Number not recognized");
        return;
    }
    
    void Save()
    {
        if (NewMember)
        {
            try
            {
                ValidateMemberID();
                String Card = "";
                for(int itr = 0; itr < 8; itr++)
                {
                    Card += (95 - Integer.valueOf(txtIdentityNumber.getText().charAt(itr))) % 10;
                }
                Calendar Cal = Calendar.getInstance();
                txtJoinDate.setText(Cal.get(Calendar.YEAR) +"-"+ String.format("%02d",Cal.get(Calendar.MONTH)) + "-" + String.format("%02d",Cal.get(Calendar.DAY_OF_MONTH)));

                String s1 = String.valueOf(Cal.get(Calendar.DAY_OF_WEEK));
                String s2 = String.valueOf(Cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));
                String s3 = String.valueOf(Cal.get(Calendar.DAY_OF_YEAR) % 10);
                String s4 = String.valueOf(Cal.get(Calendar.DAY_OF_MONTH) % 10);

                StringBuffer Buffer = new StringBuffer(Card);
                Buffer = Buffer.reverse();
                Buffer = Buffer.insert(7,s4);
                Buffer = Buffer.insert(5,s3);
                Buffer = Buffer.insert(3,s2);
                Buffer = Buffer.insert(1,s1);
                Card = Buffer.toString();

                txtLibraryCard.setText(Card);
                
                txtCardExpiry.setText("1999-01-01");
            }
            catch(Exception Ex)
            {
                JOptionPane.showMessageDialog(null, "Issue creating new member check that you entered your ID number correctly\n" + Ex.getMessage(), "Creation Warning", JOptionPane.WARNING_MESSAGE);
            }
        }
        if (RegenerateMember())
        {
            try
            {
                Connection con = MasterConnection.getInstance();
                Statement stmt = con.createStatement();
                String SQL = "";
                if (NewMember)
                    SQL = CurrentMember.InsertRecrodSQL();
                else
                    SQL = CurrentMember.UpdateRecrodSQL();
                stmt.executeUpdate(SQL);
                
                String Str = "Save successfull.";
                
                if (NewMember)
                    Str = "You have been successfully added to the database.\nTo get your printed library card please contact a librarian.";
                    
                JOptionPane.showMessageDialog(null, Str, "Success", JOptionPane.INFORMATION_MESSAGE);
            }
            catch(Exception Ex)
            {
                JOptionPane.showMessageDialog(null, "Database error, please try again later\n" + Ex.getMessage(), "Database Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        Save();
    }//GEN-LAST:event_btnSaveActionPerformed

    /**
     * Sets the current member
     * this is the method used by the other panels to select the member to display
     */
    public void SetMember(VerifiableObject VO)
    {
        if (VO == null)
            return;
        
        CurrentMember = (Member)VO;
        NewMember = false; 
        FillFields();
    }
    
    /**
     * Displays a member search panel
     */
    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        MainPanel.GetMainPanel().AddCentralUIPanel(new SearchForMember(true), this, SetMethodRef);
    }//GEN-LAST:event_btnSearchActionPerformed

    private void txtIdentityNumberActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIdentityNumberActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtIdentityNumberActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        Close();
    }//GEN-LAST:event_btnCloseActionPerformed

    /**
     * Displays a member fines panel
     */
    private void btnViewFinesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewFinesActionPerformed
        MainPanel.GetMainPanel().AddCentralUIPanel(new InformationMembershipFines(CurrentMember));
    }//GEN-LAST:event_btnViewFinesActionPerformed

    /**
     * Prints a library card for the current member
     */
    void PrintCard()
    {
        //stub
        //fixme add code to print library cards
        JOptionPane.showMessageDialog(null, "This functionality is not yet implemented.");
        
        //fixme why 2 years???
        Calendar Cal = Calendar.getInstance();
        txtCardExpiry.setText((Cal.get(Calendar.YEAR) + 2) + "-" + String.format("%02d",Cal.get(Calendar.MONTH)) + "-" + String.format("%02d",Cal.get(Calendar.DAY_OF_MONTH)));
        Save();
    }
    private void btnPrintCardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrintCardActionPerformed
        PrintCard();
    }//GEN-LAST:event_btnPrintCardActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnJoin;
    private javax.swing.JButton btnPrintCard;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnViewFines;
    private javax.swing.JComboBox cboMemberType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lstCheckouts;
    private javax.swing.JTextField txtCardExpiry;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtIdentityNumber;
    private javax.swing.JTextField txtJoinDate;
    private javax.swing.JTextField txtLibraryCard;
    private javax.swing.JTextField txtName;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JPasswordField txtPassword2;
    private javax.swing.JTextField txtPhone;
    // End of variables declaration//GEN-END:variables
}
